﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Xml;

public partial class Manage_ListLinkers : System.Web.UI.Page
{
    private List<Linker> linkers;
    private int iRow = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();
        linkers = (from aL in db.Linker
                   select aL).ToList();
    }

    protected string GenerateTable(string pid, int level)
    {
        StringBuilder sb = new StringBuilder();
        var q = linkers.Where(aL => aL.ParentID == pid).OrderBy(aL => aL.Sequence);
        foreach (Linker l in q)
        {
            if (++iRow % 2 == 0)
            {
                sb.Append("<tr class=\"even\" level=\"" + level + "\">");
            }
            else
            {
                sb.Append("<tr class=\"odd\" level=\"" + level + "\">");
            }

            //sb.Append("<td align=\"center\">");
            //sb.Append(string.Format("<input type=\"checkbox\" name=\"chkID\" value=\"{0}\" />", l.ID));
            //sb.Append("</td>");
            sb.Append("<td style=\"padding-left: " + (level * 20 + 5) + "px\">");
            sb.Append("<table cellpadding=\"0\" cellspacing=\"0\"><tr>");
            sb.Append("<td>" + string.Format("<input type=\"text\" value=\"{0}\" name=\"sequence\" style=\"width: 20px; text-align: center;\" />", l.Sequence) + "</td>");
            sb.Append("<td><input type=\"text\" name=\"name\" value=\""+l.Name + "\"/></td>");
            sb.Append("</tr></table>");
            sb.Append("</td>");
            sb.Append(string.Format("<td align=\"center\"><input type=\"text\" name=\"url\" value=\"{0}\" style=\"width: 240px\"/></td>", l.Url));
            sb.Append(string.Format("<td align=\"center\"><input type=\"text\" name=\"title\" value=\"{0}\" style=\"width: 190px\"/></td>", l.Title));
            sb.Append(string.Format("<td align=\"center\"><input type=\"radio\" value=\"_self\" name=\"rd_{0}\" id=\"rd_{0}_1\" {1}/><label for=\"rd_{0}_1\">本窗口</label>&nbsp;&nbsp;<input type=\"radio\" value=\"_blank\" name=\"rd_{0}\" {2}/><label for=\"rd_{0}_2\">新窗口</label></td>", l.ID, (l.Target == "_self") ? "checked=\"checked\"" : "", (l.Target == "_blank") ? "checked=\"checked\"" : ""));
            sb.Append("</tr>");

            sb.Append(GenerateTable(l.ID, level + 1));
        }

        return sb.ToString();
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string xml = LinkerXML.Value;
        xml = xml.Replace("&lt;", "<").Replace("&gt;", ">");
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);

        CmsDataContext db = new CmsDataContext();
        
        try
        {   
            var q1 = from aL in db.Linker
                     select aL;
            db.Linker.DeleteAllOnSubmit(q1);

            XmlNodeList nodes = doc.SelectNodes("/linkers/linker");
            foreach (XmlNode node in nodes)
            {
                Linker l = new Linker();
                l.ID = node.Attributes["id"].Value;
                l.Sequence = Convert.ToByte(node.SelectSingleNode("sequence").InnerText);
                l.Name = node.SelectSingleNode("name").InnerText;
                l.Url = node.SelectSingleNode("url").InnerText;
                l.Title = node.SelectSingleNode("title").InnerText;
                l.Target = node.SelectSingleNode("target").InnerText;

                string pid = node.SelectSingleNode("parent").InnerText;

                l.ParentID = pid == "0" ? null : pid;

                l.Icon = "";
                l.Type = "";

                db.Linker.InsertOnSubmit(l);
            }

            db.SubmitChanges();

            MessageBox.Message = "导航栏信息保存成功。";
            MessageBox.Location = "./ListLinkers.aspx";
        }
        catch
        {
            MessageBox.Message = "保存导航栏信息时发生错误。";
            MessageBox.Location = "./ListLinkers.aspx";
        }
    }
}